package com.mrjy.common.qtz;

import java.text.SimpleDateFormat;
import java.util.List;

import javax.annotation.Resource;

import org.apache.commons.lang.StringUtils;
import org.apache.log4j.Logger;

import com.mrjy.activity.dao.IActivityInfoDao;
import com.mrjy.activity.dto.ActivityOutputDTO;
import com.mrjy.common.entity.ActivityInfo;
import com.mrjy.common.entity.UserInfo;
import com.mrjy.common.thread.ActivityThreadTask;
import com.mrjy.common.thread.ThreadTask;
import com.mrjy.common.thread.TimingThreadPool;
import com.mrjy.maintain.dao.IUserInfoDao;

/**
 * 活动上新微信通知
 *
 * @author csshui
 * @date 2018-08-1
 */
public class ActivityListJob {
    public static Integer counter_a = 0;
    
    public static Integer couter_b = 0;
    
    private static Logger logger = Logger.getLogger(ActivityListJob.class);
    @Resource
    private IActivityInfoDao activityInfoDao;
    @Resource
    private IUserInfoDao userInfoDao ;
    /**
     * @param
     * @Description: 任务执行
     */
    public void doJob() {
        
        synchronized (ActivityListJob.counter_a) {
            logger.debug("活动通知处理 ：=================================>");
            ActivityInfo aiParam = new ActivityInfo();
            aiParam.setDisableFlag(0);
            List<ActivityOutputDTO> list = activityInfoDao.queryList(aiParam);
             
            SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            for (ActivityOutputDTO info : list) {
            	if(info != null && info.getNoticeTime()!= null){
            		//如果通知时间和系统时间相差2分钟内，则通知
            		if(Math.abs((System.currentTimeMillis() - info.getNoticeTime().getTime())/(1000*60))<= 2){
            			String activityType=  "直播课" ;
            			if("1".equals(info.getActivityType())){
            				activityType = "直播课" ;
            			}else if("2".equals(info.getActivityType())){
            				activityType = "线上课" ;
            			}else if("3".equals(info.getActivityType())){
            				activityType = "线下课" ;
            			}
            			List<UserInfo> userList = userInfoDao.queryWeixinOpenidList() ;
            			for(UserInfo u : userList){
            				 // 产生一个任务，并将其加入到线程池
							try {
								ThreadTask threadTask= ThreadTask.clonePolyInfo(info.getId(),null
										,activityType,info.getTeacherNames() ,null,null
										,info.getActivityName(),null,u.getWeixinOpenid() 
										,formatter.format(info.getStartTime()),3, info.getNoticeTitleDesc(), info.getNoticeFootDesc());
								TimingThreadPool.pool.execute(threadTask);
							} catch (CloneNotSupportedException e) {
								e.printStackTrace();
							}
                            logger.debug("微信模板消息发送成功 ==========> 已向" + u.getWeixinOpenid() + "用户推送微信通知");
            			}
            		}
            	}
            	
                
            }
        }

    }
    
    
    public void doSendMessage(){
    	synchronized (ActivityListJob.couter_b) {
    		logger.debug("活动通知处理 ：===========doSendMessage======================>");
    		List<UserInfo> userList = userInfoDao.queryWeixinOpenidList() ;
    		for(UserInfo u : userList){
        		try {
        			if(StringUtils.isNotBlank(u.getWeixinOpenid())){
        				ActivityThreadTask att = new  ActivityThreadTask(u.getWeixinOpenid());
        				TimingThreadPool.pool.execute(att);
        			}
    			} catch (Exception e) {
    				e.printStackTrace();
    			}
    		}
		}
    	
    	
	}
    
    
}